package com.recharge.web.controller;


import com.recharge.common.enums.ErrorCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.recharge.common.annotation.OperationLogDetail;
import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.OperationLogType;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.GoodsGroupQueryDO;
import com.recharge.domain.vo.GoodsDO;
import com.recharge.domain.vo.GoodsGroupDO;
import com.recharge.service.GoodsGroupService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 商品分组 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/goods-group")
public class GoodsGroupController extends BaseController {
	
	@Autowired
    private GoodsGroupService goodsGroupService;

	@RequiresPermissions("goods-group:page")
	@RequestMapping(value="/page.html", method = RequestMethod.POST)
	 @ApiOperation(value="分页查询商品组", notes="")
	public @ResponseBody  ObjectResponse<PageDO> page(@RequestBody GoodsGroupQueryDO query) {

	 	ObjectResponse<PageDO> response = new ObjectResponse<>();

	 	ResultDO result = goodsGroupService.find(query);

	 	if(result.isSuccess()) {
			response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}

	 	return response;
	 }


	@RequiresPermissions("goods-group:add")
	@OperationLogDetail(detail = "添加商品组", operationType = OperationLogType.ADD)
	@RequestMapping(value="/add.html", method = RequestMethod.POST)
	@ApiOperation(value="添加商品组", notes="")
	public @ResponseBody  ObjectResponse<Object> add(@RequestBody GoodsGroupDO param) {
	 	ObjectResponse<Object> response = new ObjectResponse<>();
	 	
	 	param.setCreator(getUserName());
	 	
	 	ResultDO result = goodsGroupService.add(param);
	 	if(result.isSuccess()) {
			response.setData(null);
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
	 	return response;
	 }

	@RequiresPermissions("goods-group:delete")
	@OperationLogDetail(detail = "删除商品组", operationType = OperationLogType.DELETE)
	@RequestMapping(value="/delete.html", method = RequestMethod.POST)
	@ApiOperation(value="删除商品组", notes="")
	public @ResponseBody  ObjectResponse<Object> delete(@RequestBody GoodsGroupDO param) {
	 	ObjectResponse<Object> response = new ObjectResponse<>();
	 	
	 	ResultDO result = goodsGroupService.remove(param.getId(), getUserName());
	 	if(result.isSuccess()) {
			response.setData(null);
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
	 	return response;
	 }


	@RequiresPermissions("goods-group:update")
	@OperationLogDetail(detail = "修改商品组", operationType = OperationLogType.UPDATE)
	@RequestMapping(value="/update.html", method = RequestMethod.POST)
	@ApiOperation(value="修改商品组", notes="")
	public @ResponseBody  ObjectResponse<Object> update(@RequestBody GoodsGroupDO param) {
	 	ObjectResponse<Object> response = new ObjectResponse<>();
	 	
	 	param.setModifier(getUserName());
	 	
	 	ResultDO result = goodsGroupService.edit(param);
	 	if(result.isSuccess()) {
			response.setData(null);
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
	 	return response;
	 }

	@RequiresPermissions("goods-group:get")
	@RequestMapping(value="/get.html", method = RequestMethod.POST)
	@ApiOperation(value="查询商品组详情", notes="")
	public @ResponseBody  ObjectResponse<Object> get(@RequestBody GoodsGroupDO param) {
	 	ObjectResponse<Object> response = new ObjectResponse<>();
	 	ResultDO result = goodsGroupService.get(param.getId());
	 	if(result.isSuccess()) {
			response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
	 	return response;
	 }

	@RequiresPermissions("goods-group:batchDisassociate")
	@RequestMapping(value="/batchDisassociate.html", method = RequestMethod.POST)
	@ApiOperation(value="批量取消商品组关联商品", notes="")
	public @ResponseBody  ObjectResponse<Object> batchDisassociate(@RequestBody GoodsGroupDO param) {
		ObjectResponse<Object> response = new ObjectResponse<>();
		if(StringUtils.isEmpty(param.getGoodsIds())) {
			response.setStatus(ErrorCode.PARAM_GET_ERROR.getCode());
			response.setMsg(ErrorCode.PARAM_GET_ERROR.getMsg());
		} else {
			ResultDO result = goodsGroupService.batchDisassociate(param.getId(), param.getGoodsIds().split(","), getUserName());
			if(result.isSuccess()) {
				response.setData(null);
			} else {
				response.setStatus(result.getErrorCode().getCode());
				response.setMsg(result.getErrorCode().getMsg());
			}
		}
		return response;
	}
}
